<template>
  <div class="container">
    <el-form class="form" size="large" :model="adminForm">
      <div class="title-container">
        <h3>天上租房后台管理系统</h3>
      </div>
      <el-form-item prop="username">
        <el-input :prefix-icon="User" type="text" v-model="adminForm.adminName" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input :prefix-icon="Lock" type="password" v-model="adminForm.adminPassword"
                  placeholder="请输入密码"></el-input>
      </el-form-item>
      <!--由于el-button是放在表单里，默认是会实现表单的跳转，需要用@click.native.prevent阻止-->
      <el-button class="button" type="primary"
                 @click.native.prevent="handleLogin">登录
      </el-button>
    </el-form>
  </div>
</template>

<script setup>
import {reactive} from 'vue';
import {useRouter} from "vue-router"
import sysAxios from "@/api/sys.js"
import {useDataStore} from "@/stores/dataStore"
import {Lock, User} from '@element-plus/icons-vue' //输入框引入字体图标，使用:prefix-icon="User"
const dataStore = useDataStore()
const router = useRouter()

//用户表单
const adminForm = reactive({
  adminName: '',
  adminPassword: '',
})
/**
 * 登录
 */
const handleLogin = () => {
  if (adminForm.adminName.length >= 1 || adminForm.adminPassword.length >= 1) {
    sysAxios.sys_login({
      username: adminForm.adminName,
      password: adminForm.adminPassword
    }).then(res => {
      if (res.data.code === 200) {
        dataStore.adminName = res.data.data.username;
        sysAxios.admin_findByAdminName({
          adminName: res.data.data.username
        }).then(res => {
          dataStore.adminId = res.data.data.adminId
          dataStore.adminImg = res.data.data.adminImg
        })
        dataStore.islogin = true
        router.push("/sys/admin")
      } else {
        ElMessage.error(res.data.message)
      }
    })
  } else {
    ElMessage.error('账号密码不能为空')
  }

}
</script>
<style scoped lang="scss">
.container {
  width: 100%;
  height: 800px;
  background-image: linear-gradient(120deg, #00e4d0, #5983e8) !important;

  .form {
    position: relative;
    width: 400px;
    padding: 160px 35px 0;
    margin: 0 auto;

    .title-container {
      font-size: 30px;
      color: #fff;
      text-align: center;
      margin: 20px;
    }

    .button {
      width: 100%;
      margin-bottom: 30px;
    }
  }
}

</style>
